<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>浪起BI-ETL平台：登录</title>
    <!-- 引入 Tailwind CSS -->
    <script src="https://cdn.tailwindcss.com"></script>
    <!-- 引入 Font Awesome 图标库 -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        body {
            margin: 0;
            font-family: Arial, sans-serif;
            background: linear-gradient(135deg, #007BFF, #00C6FF);
            height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        .login-container {
            background: white;
            border-radius: 10px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            padding: 30px;
            width: 350px;
        }
        .login-container h1 {
            text-align: center;
            font-size: 24px;
            font-weight: bold;
            margin-bottom: 20px;
            color: #007BFF;
        }
        .login-container button:hover {
            opacity: 0.9;
        }
        footer {
            position: absolute;
            bottom: 10px;
            text-align: center;
            width: 100%;
            color: white;
            font-size: 14px;
        }
    </style>
</head>
<body>
    <div class="login-container">
        <h1>BI-ETL平台</h1>
        <form id="loginForm">
            <div class="mb-4">
                <label for="username" class="block text-gray-700 text-sm font-bold mb-2">账号</label>
                <input type="text" id="username" name="username" placeholder="请输入账号" class="w-full px-3 py-2 border border-gray-300 rounded focus:outline-none focus:ring-2 focus:ring-blue-500" required>
            </div>
            <div class="mb-4">
                <label for="password" class="block text-gray-700 text-sm font-bold mb-2">密码</label>
                <input type="password" id="password" name="password" placeholder="请输入密码" class="w-full px-3 py-2 border border-gray-300 rounded focus:outline-none focus:ring-2 focus:ring-blue-500" required>
            </div>
            <div id="error-message" class="text-red-500 text-sm mb-4 hidden"></div>
            <div class="flex justify-between items-center">
                <button type="submit" id="loginBtn" class="w-full bg-blue-500 text-white py-2 px-4 rounded hover:bg-blue-600">登录</button>
                <button type="button" id="cancelBtn" class="w-full bg-gray-300 text-black py-2 px-4 rounded ml-2 hover:bg-gray-400">取消</button>
            </div>
        </form>
    </div>

    <script>
        document.getElementById('loginForm').addEventListener('submit', async function(e) {
            e.preventDefault();
            
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            const errorMessage = document.getElementById('error-message');
            const loginBtn = document.getElementById('loginBtn');
            
            if (!username || !password) {
                errorMessage.textContent = '请输入用户名和密码';
                errorMessage.classList.remove('hidden');
                return;
            }
            
            loginBtn.disabled = true;
            loginBtn.textContent = '登录中...';
            
            try {
                const response = await fetch('/api/login', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        username: username,
                        password: password
                    })
                });
                
                const data = await response.json();
                
                if (data.success) {
                    // 登录成功，跳转到主页
                    window.location.href = '/';
                } else {
                    errorMessage.textContent = data.message || '登录失败';
                    errorMessage.classList.remove('hidden');
                }
            } catch (error) {
                errorMessage.textContent = '网络错误，请稍后重试';
                errorMessage.classList.remove('hidden');
            } finally {
                loginBtn.disabled = false;
                loginBtn.textContent = '登录';
            }
        });
        
        document.getElementById('cancelBtn').addEventListener('click', function() {
            document.getElementById('username').value = '';
            document.getElementById('password').value = '';
            document.getElementById('error-message').classList.add('hidden');
        });
    </script>
    <footer>
        © 2025 浪起科技
    </footer>
</body>
</html>